/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package graphics.support;

import graphics.Kineticsgraph;
import myarrays.MyArrays;
import graphics.Maingraph;
import graphics.Spectragraph;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.Arrays;
import javax.swing.SwingUtilities;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYBoxAnnotation;
import org.jfree.chart.panel.CrosshairOverlay;
import org.jfree.chart.plot.Crosshair;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBlockRenderer;

/**
 *
 * @author asus
 */
public class MyEvent implements MouseListener, MouseMotionListener, KeyListener {

    private Spectragraph spectra;
    private Kineticsgraph kinetics;
    private ChartPanel chartPanel;
    private JFreeChart chart;
    private Crosshair crosshair1X;
    private Crosshair crosshair1Y;
    private Crosshair crosshair2X;
    private Crosshair crosshair2Y;
    private CrosshairOverlay overlay;
    private XYPlot plot;
    private XYBlockRenderer renderer;

    private Maingraph MainGraph;

    private Boolean mouse_dragged;
    private Boolean CrossHair_chossen;
    private Boolean shift_down = false;
    private Boolean ctrl_down = false;
    private Boolean double_clicked = false;
    private String WhichCrossHair;

    double[] XY = new double[2];
    double[] varXY = new double[2];
    private double[][] a = new double[2][2];

    MouseEvent e;

    private final Color transblue;

    public MyEvent(Maingraph graph) {
        this.WhichCrossHair = "";
        this.transblue = new Color(0, 0, 255, 60);
        this.MainGraph = graph;
        this.overlay = graph.getOverlay();
        this.chartPanel = graph.getChartPanel();
        this.chart = graph.getJFreeChart();
        this.crosshair1X = graph.getCrossHairs().get(0);
        this.crosshair1Y = graph.getCrossHairs().get(1);
        this.crosshair2X = graph.getCrossHairs().get(2);
        this.crosshair2Y = graph.getCrossHairs().get(3);
        this.spectra = graph.getSpectraGraph();
        this.kinetics = graph.getKineticsGraph();
        this.renderer = graph.getRenderer();
        this.plot = graph.getPlot();
        mouse_dragged = false;
        CrossHair_chossen = false;
        a[0][0] = crosshair1X.getValue();
        a[1][0] = crosshair1Y.getValue();
        a[0][1] = crosshair2X.getValue();
        a[1][1] = crosshair2Y.getValue();
        setAnnotation();
    }

    @Override
    public void mouseClicked(MouseEvent me) {
        if (me.getClickCount() == 2) {
        }
    }

    @Override
    public void mousePressed(MouseEvent me) {

    }

    @Override
    public void mouseReleased(MouseEvent me) {
        if (ctrl_down) {
            ctrl_down = false;
            setAnnotation();
            chartPanel.restoreAutoBounds();
            mouse_dragged = false;
        }
    }

    @Override
    public void mouseEntered(MouseEvent me) {
        chartPanel.requestDefaultFocus();
    }

    @Override
    public void mouseExited(MouseEvent me) {
    }

    private double[] getmouseposition(MouseEvent e) {
        double[] var = new double[2];
        Point2D p = chartPanel.translateScreenToJava2D(e.getPoint());
        Rectangle2D plotArea = chartPanel.getScreenDataArea();
        XYPlot varplot = (XYPlot) chart.getPlot(); // your plot
        var[0] = varplot.getDomainAxis().java2DToValue(p.getX(), plotArea, varplot.getDomainAxisEdge());
        var[1] = varplot.getRangeAxis().java2DToValue(p.getY(), plotArea, varplot.getRangeAxisEdge());
        return var;
    }

    @Override
    public void mouseDragged(MouseEvent me) {
        if (SwingUtilities.isLeftMouseButton(me) && ctrl_down) {
            if (mouse_dragged == false) {
                XY = getmouseposition(me);
                mouse_dragged = true;
            }
            if (mouse_dragged == true) {
                varXY = getmouseposition(me);
                if (XY[0] < varXY[0]) {
                    a[0][0] = XY[0];
                    a[0][1] = varXY[0];
                }
                if (XY[0] >= varXY[0]) {
                    a[0][0] = varXY[0];
                    a[0][1] = XY[0];
                }
                if (XY[1] < varXY[1]) {
                    a[1][0] = XY[1];
                    a[1][1] = varXY[1];
                }
                if (XY[1] >= varXY[1]) {
                    a[1][0] = varXY[1];
                    a[1][1] = XY[1];
                }
            }
            setAnnotation();
        }
    }

    @Override
    public void mouseMoved(MouseEvent me) {
        e = me;
        if (shift_down) {
            varXY = getmouseposition(e);
            if (WhichCrossHair == "X1") {
                crosshair1X.setValue(varXY[0]);
                a[0][0] = varXY[0];
                Arrays.sort(a[0]);
            }
            if (WhichCrossHair == "X2") {
                crosshair2X.setValue(varXY[0]);
                a[0][1] = varXY[0];
                Arrays.sort(a[0]);
            }
            if (WhichCrossHair == "Y1") {
                crosshair1Y.setValue(varXY[1]);
                a[1][0] = varXY[1];
                Arrays.sort(a[1]);
            }
            if (WhichCrossHair == "Y2") {
                crosshair2Y.setValue(varXY[1]);
                a[1][1] = varXY[1];
                Arrays.sort(a[1]);
            }
            setAnnotation();
        }
    }

    @Override
    public void keyTyped(KeyEvent ke
    ) {

    }

    @Override
    public void keyPressed(KeyEvent ke) {

        if (ke.getKeyCode() == 16 && shift_down != true) {
            shift_down = true;
            WhichCrossHair = FindCrossHair.findclosest(crosshair1X, crosshair2X, crosshair1Y, crosshair2Y, getmouseposition(e));

            if ("X1".equals(WhichCrossHair)) {
                crosshair1X.setValue(getmouseposition(e)[0]);
            }
            if ("X2".equals(WhichCrossHair)) {
                crosshair2X.setValue(getmouseposition(e)[0]);
            }
            if ("Y1".equals(WhichCrossHair)) {
                crosshair1Y.setValue(getmouseposition(e)[1]);
            }
            if ("Y2".equals(WhichCrossHair)) {
                crosshair2Y.setValue(getmouseposition(e)[1]);
            }
            MainGraph.setCursor(new Cursor(Cursor.MOVE_CURSOR));
        }

        if (ke.getKeyCode() == 17 && ctrl_down != true) {
            ctrl_down = true;
        }
    }

    @Override
    public void keyReleased(KeyEvent ke
    ) {
        if (ke.getKeyCode() == 16) {
            shift_down = false;
            MainGraph.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        }
        if (ke.getKeyCode() == 17) {
        }
    }

    public void setAnnotation() {
        a[0] = MyArrays.notexciding(a[0], MainGraph.getMinWavelength(), MainGraph.getMaxWavelength());
        a[1] = MyArrays.notexciding(a[1], MainGraph.getMinTimedelay(), MainGraph.getMaxTimedelay());
        MainGraph.setWorkingArea(a);
        spectra.setWorkingArea(a);
        kinetics.setWorkingArea(a);
        renderer.removeAnnotations();
        renderer.addAnnotation(new XYBoxAnnotation(a[0][0], a[1][0], a[0][1], a[1][1], new BasicStroke(1), Color.BLACK, transblue));
        MainGraph.setRenderer(renderer);
        crosshair1X.setValue(a[0][0]);
        crosshair2X.setValue(a[0][1]);
        crosshair1Y.setValue(a[1][0]);
        crosshair2Y.setValue(a[1][1]);
    }
}
